package com.bjsxt.net.tcp3;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

//服务器端实现runnable接口的类
public class ServerRunnable implements Runnable {

    private Socket client;

    public ServerRunnable(Socket client){
        this.client = client;
    }
    @Override
    public void run() {
        try{
            boolean flag = false;
            //用于接收客户端发送的数据
            ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
            //用于向客户端发送反馈数据
            DataOutputStream dos = new DataOutputStream(client.getOutputStream());

            while(true){

                User user = (User) ois.readObject();

                if(user.getUsername().equals("bjsxt") && user.getPassword().equals("bjsxt")){
                    System.out.println(user.getUsername() + "登录成功");
                    flag = true;
                }else{
                    System.out.println(user.getUsername()+"-------->"+user.getPassword()+"登录失败");
                }

                //给客户端反馈
                dos.writeBoolean(flag);
                if(flag){
                    break;//登录成功，则不需要再接收客户端数据了，结束循环
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
